#include "changeview.h"
#include "ui_changeview.h"
#include "config.h"
#include "sqlcontroller.h"
#include "maincontroller.h"
ChangeView::ChangeView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ChangeView)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("修改页面"));

    //货物属性回显
    ProductInfo chpro=SqlController::getSqlController()->getTempPro();
    ui->Edit_name->setText(chpro.name);
    ui->Edit_nums->setText(QString::number(chpro.nums));
    ui->Edit_classify->setText(chpro.classify);
    ui->Edit_price->setText(QString::number(chpro.price));
    ui->Edit_weight->setText(QString::number(chpro.weight));
    ui->Edit_supplier->setText(chpro.supplier);
    //记录修改货物id
    id=chpro.id;

    //关闭对话框时删除该窗体对象
    connect(this,&QDialog::finished,this,[](){
        MainController::getMainController()->deleteChangView();
    });
}

ChangeView::~ChangeView()
{

    delete ui;
}




void ChangeView::on_buttonBox_clicked(QAbstractButton *button)
{
    if(button->text()=="OK"){
        ProductInfo newPro;
        newPro.name=ui->Edit_name->text();
        newPro.nums=ui->Edit_nums->text().toInt();
        newPro.classify=ui->Edit_classify->text();
        newPro.price=ui->Edit_price->text().toDouble();
        newPro.weight=ui->Edit_weight->text().toDouble();
        newPro.supplier=ui->Edit_supplier->text();
        newPro.user_id=SqlController::getSqlController()->getUser_id();
        SqlController::getSqlController()->updateProduct(newPro,id);
        MainController::getMainController()->deleteChangView();

    }else if(button->text()=="Cancel"){
        MainController::getMainController()->deleteChangView();
    }
}

